package com.ruoyi.manage.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.manage.domain.WareHouse;
import com.ruoyi.manage.service.WareHouseService;
import com.ruoyi.system.api.domain.SysOperLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 满杯百香果🍎
 */
@RestController
@RequestMapping("/warehouse")
public class WarehouseController extends BaseController {

    @Autowired
    private WareHouseService wareHouseService;

    @GetMapping("/list")
    public TableDataInfo getList(WareHouse wareHouse){
        startPage();
        List<WareHouse> list = wareHouseService.getList(wareHouse);
        return getDataTable(list);
    }

    /** 获取机构名 */
    @GetMapping("/getMechanismName")
    public TableDataInfo getMechanismName(){
        List<WareHouse> mechanismName = wareHouseService.getMechanismName();
        return getDataTable(mechanismName);
    }

    /** 新增仓库 */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody WareHouse wareHouse){
        return wareHouseService.add(wareHouse);
    }

    /** 新增仓库 */
    @DeleteMapping("/del/{id}")
    public AjaxResult del(@PathVariable("id") Integer id){
        return wareHouseService.del(id);
    }

    /** 合并仓库 */
    @PutMapping("/together")
    public AjaxResult together(@RequestBody List<WareHouse> list){
        if (wareHouseService.together(list) > 0){
            return AjaxResult.success("合并成功！");
        }
        return AjaxResult.error("合并失败！");
    }

    @Log(title = "仓库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WareHouse wareHouse)
    {
        List<WareHouse> list = wareHouseService.getList(wareHouse);
        ExcelUtil<WareHouse> util = new ExcelUtil<WareHouse>(WareHouse.class);
        util.exportExcel(response, list, "仓库信息");
    }


}
